function onDeviceManageLoad() {
     var tableBuilder = new DeviceTableBuilder();
    // tableBuilder.addDeviceRow(1, "1", "温度传感器", "2020-3-12 10:10:10", false, 0);
    // tableBuilder.addDeviceRow(2, "2", "门磁传感器", "2021-5-5 12:34:56", true, 1);
    // tableBuilder.build();
	//let tableBuilder = new UserTableBuilder();
	let selectBuilder108 = new DeviceSelectBuilder();
	$.ajax({
	    url:URL_HEAD + "device/findAll",
	    //通过type来判断调用哪个方法
	    type: "post",
	    dataType: "json",
	    success: function (result){
			console.log(result.data);
			for(let i=0; i < result.data.length;i++){
				tableBuilder.addDeviceRow(i+1,result.data[i]._id,result.data[i].
										devicename,result.data[i].updatetime,
										result.data[i].status,result.data[i].type);
				selectBuilder108.addDeviceSelect108(result.data[i].devicename);						
				if(i==0){
					document.getElementById("u108_input").value = result.data[0].devicename;
				}						
			}
			tableBuilder.build();
			selectBuilder108.buildSelct108();
	    },
	    error: function (xhr, textStatus, errorThrown){
	        showError("刷新异常", xhr, textStatus, errorThrown);
	    }
	});
}
function deleteValue(){
	const idStr = document.getElementById("u153_input").value;
	$.ajax({
	    url:URL_HEAD + "device/deleteValue",
	    //通过type来判断调用哪个方法
	    type: "post",
		data: {"idStr": idStr},
	    dataType: "json",
	    success: function (result){
				location.reload();
	    },
	    error: function (xhr, textStatus, errorThrown){
	        showError("刷新异常", xhr, textStatus, errorThrown);
	    }
	});
}
function addDeviceValue(){
	const devicename = document.getElementById("u141_input").value;
	const value = document.getElementById("u145_input").value;
	$.ajax({
	    url:URL_HEAD + "device/addDeviceValue",
	    //通过type来判断调用哪个方法
	    type: "post",
		data: {"devicename": devicename , "value": value},
	    dataType: "json",
	    success: function (result){
			location.reload();
	    },
	    error: function (xhr, textStatus, errorThrown){
	        showError("刷新异常", xhr, textStatus, errorThrown);
	    }
	});
}
function onDevelopDeviceManageLoad() {
    //var tableBuilder = new DeviceTableBuilder();
    // tableBuilder.addDeviceRow(1, "1", "温度传感器", "2020-3-12 10:10:10", false, 0);
    // tableBuilder.addDeviceRow(2, "2", "门磁传感器", "2021-5-5 12:34:56", true, 1);
    // tableBuilder.build();
	let selectBuilder141 = new DeviceSelectBuilder();
	let selectBuilder155 = new DeviceSelectBuilder();
	$.ajax({
	    url:URL_HEAD + "device/findAll",
	    //通过type来判断调用哪个方法
	    type: "post",
	    dataType: "json",
	    success: function (result){
			for(let i=0; i < result.data.length;i++){
				if(i==0){
					document.getElementById("u145_input").value = result.data[0].value;
					document.getElementById("u153_input").value = result.data[0].idString;
					document.getElementById("u159_input").value = result.data[0].value;
				}
				selectBuilder141.addDeviceSelect141(result.data[i].devicename);	
				selectBuilder155.addDeviceSelect155(result.data[i].devicename);
			}
			selectBuilder141.buildSelct141()
			selectBuilder155.buildSelct155();
	    },
	    error: function (xhr, textStatus, errorThrown){
	        showError("刷新异常", xhr, textStatus, errorThrown);
	    }
	});
}
function showValue108(){
	const devicename = document.getElementById("u108_input").value;
	$.ajax({
	    url:URL_HEAD + "device/findOneInDeviceName",
	    //通过type来判断调用哪个方法
	    type: "post",
		data: {"devicename": devicename},
	    dataType: "json",
	    success: function (result){
			alert(devicename + "的数据为: " + result.data[0].value);
	    },
	    error: function (xhr, textStatus, errorThrown){
	        showError("刷新异常", xhr, textStatus, errorThrown);
	    }
	});
}
function showValue141(){
	const devicename = document.getElementById("u141_input").value;
	$.ajax({
	    url:URL_HEAD + "device/findOneInDeviceName",
	    //通过type来判断调用哪个方法
	    type: "post",
		data: {"devicename": devicename},
	    dataType: "json",
	    success: function (result){
			document.getElementById("u145_input").value = result.data[0].value;
	    },
	    error: function (xhr, textStatus, errorThrown){
	        showError("刷新异常", xhr, textStatus, errorThrown);
	    }
	});
}
function showValue159(){
	const devicename = document.getElementById("u155_input").value;
	$.ajax({
	    url:URL_HEAD + "device/findOneInDeviceName",
	    //通过type来判断调用哪个方法
	    type: "post",
		data: {"devicename": devicename},
	    dataType: "json",
	    success: function (result){
			document.getElementById("u159_input").value = result.data[0].value;
	    },
	    error: function (xhr, textStatus, errorThrown){
	        showError("刷新异常", xhr, textStatus, errorThrown);
	    }
	});
}
function DeviceSelectBuilder() {
    this.builder = new StringBuilder();
    this.interval = 25;
}
DeviceSelectBuilder.prototype.addDeviceSelect108 = function (devicename){
	this.builder.append('<option class=u108_input_option ');
	this.builder.append('value="');
	this.builder.append(devicename);
	this.builder.append('">');
	this.builder.append(devicename);
	this.builder.append('</option>');
}
DeviceSelectBuilder.prototype.buildSelct108 = function () {
    var value = this.builder.toString();
    $("#u108_input").append(value);
}
DeviceSelectBuilder.prototype.addDeviceSelect141 = function (devicename){
	this.builder.append('<option class=u141_input_option ');
	this.builder.append('value="');
	this.builder.append(devicename);
	this.builder.append('">');
	this.builder.append(devicename);
	this.builder.append('</option>');
}
DeviceSelectBuilder.prototype.buildSelct141 = function () {
    var value = this.builder.toString();
    $("#u141_input").append(value);
}

// function addDeviceSel155() {
//     //var tableBuilder = new DeviceTableBuilder();
//     // tableBuilder.addDeviceRow(1, "1", "温度传感器", "2020-3-12 10:10:10", false, 0);
//     // tableBuilder.addDeviceRow(2, "2", "门磁传感器", "2021-5-5 12:34:56", true, 1);
//     // tableBuilder.build();
// 	let selectBuilder = new DeviceSelectBuilder();
// 	$.ajax({
// 	    url:URL_HEAD + "device/findAll",
// 	    //通过type来判断调用哪个方法
// 	    type: "post",
// 	    dataType: "json",
// 	    success: function (result){
// 			for(let i=0; i < result.data.length;i++){
// 				selectBuilder.addDeviceSelect155(result.data[i].devicename);			
// 			}
// 			selectBuilder.buildSelct155();
// 	    },
// 	    error: function (xhr, textStatus, errorThrown){
// 	        showError("刷新异常", xhr, textStatus, errorThrown);
// 	    }
// 	});
// }

DeviceSelectBuilder.prototype.addDeviceSelect155 = function (devicename){
	this.builder.append('<option class=u155_input_option ');
	this.builder.append('value="');
	this.builder.append(devicename);
	this.builder.append('">');
	this.builder.append(devicename);
	this.builder.append('</option>');
}
DeviceSelectBuilder.prototype.buildSelct155 = function () {
    var value = this.builder.toString();
    $("#u155_input").append(value);
}

function addDevice(){
	const devicename = document.getElementById("u125_input").value;
	const statusValue = document.getElementById("u130_input").value;
	if(statusValue == "是"){
		var status = 1;
	}else{
		var status = 0;
	}
	const typeValue = document.getElementById("u126_input").value;
	if(typeValue == "数值测量值型"){
		var type = 0;
	}else if(typeValue == "开关状态型"){
		var type = 1;
	}else if(typeValue == "地理位置定位型"){
		var type = 2;
	}else{
		var type = 3;
	}
	$.ajax({
	    url:URL_HEAD + "device/addDevice",
	    //通过type来判断调用哪个方法
	    type: "post",
	    data: {"devicename": devicename, "status": status, "type": type},
	    dataType: "json",
	    success: function (result){
			onDeviceManageLoad();
			document.getElementById("u125_input").value = " ";
	    },
	    error: function(xhr, textStatus, errorThrown){
	        showError("用户删除异常", xhr, textStatus, errorThrown);
	    }
	});
}

function onModifyDeviceClick(index) {
	const id = document.getElementById("rId-" + index).innerText;
	const devicename = document.getElementById("txtName-" + index).value;
	const updatetime = document.getElementById("rLatestTimestamp-" + index).innerText;
	const status = document.getElementById("selOnline-" + index).value;
	const type = document.getElementById("selDataType-" + index).value;
	$.ajax({
	    url:URL_HEAD + "device/modifyDevice",
	    //通过type来判断调用哪个方法
	    type: "post",
		data: {"id": id , "devicename": devicename , "updatetime": updatetime, "status": status , "type": type},
	    dataType: "json",
	    success: function (result){
			location.reload();
			document.getElementById("u90_input").value = " ";
			document.getElementById("u91_input").value = " ";
			document.getElementById("u92_input").value = " ";
			document.getElementById("u94_input").value = " ";
	    },
	    error: function(xhr, textStatus, errorThrown){
	        showError("设备修改异常", xhr, textStatus, errorThrown);
	    }
	});
}

function onDeleteDeviceClick(index) {
	const id = document.getElementById("rId-" + index).innerText;
	$.ajax({
	    url:URL_HEAD + "device/deleteDevice",
	    //通过type来判断调用哪个方法
	    type: "post",
		data: {"id": id},
	    dataType: "json",
	    success: function (result){
			location.reload();
			document.getElementById("u90_input").value = " ";
			document.getElementById("u91_input").value = " ";
			document.getElementById("u92_input").value = " ";
			document.getElementById("u94_input").value = " ";
	    },
	    error: function(xhr, textStatus, errorThrown){
	        showError("设备修改异常", xhr, textStatus, errorThrown);
	    }
	});
}

/**
 * 该类实现设备表格动态添加行
 */
function DeviceTableBuilder() {
    this.builder = new StringBuilder();
    this.interval = 25;

    $("#u111-data").remove();
    this.builder.append('<div id="u111-data">');
}


DeviceTableBuilder.prototype.addDeviceRow = function (index, id, name, timestamp, online, dataType) {
    var rIdId = "rId-" + index;
    var txtNameId = "txtName-" + index;
    var rLatestTimestampId = "rLatestTimestamp-" + index;
    var selOnlineId = "selOnline-" + index;
    var selDataTypeId = "selDataType-" + index;
    var btnModifyDeviceId = "btnModifyDevice-" + index;
    var btnDeleteDeviceId = "btnDeleteDevice-" + index;
    var rowTop = this.interval * (index - 1);

    //新的设备信息行
    this.builder.append('<div class="preeval" style="width: 561px; height: 25px;">');
    //设备ID
    this.builder.append('<div class="ax_default box_1 u116" data-label="rId" ');
    this.builder.append('style="width: 100px; height: 25px; left: 0px; top: ');
    this.builder.append(rowTop);
    this.builder.append('px;visibility: inherit">');
    this.builder.append('<div class="u116_div" style="width: 100px; height: 25px;visibility: inherit"></div>');
    this.builder.append('<div class="text u116_text" style="visibility: inherit">');
    this.builder.append('<p id="');
    this.builder.append(rIdId);
    this.builder.append('"><span style="text-decoration:none;">');
    this.builder.append(id);
    this.builder.append('</span></p>');
    this.builder.append('</div>');
    this.builder.append('</div>');
    //设备名称
    this.builder.append('<div class="ax_default text_field u112" data-label="txtName"');
    this.builder.append(' style="width: 100px; height: 25px; left: 44px; top: ');
    this.builder.append(rowTop);
    this.builder.append('px;visibility: inherit">');
    this.builder.append('<div class="u112_div" style="width: 100px; height: 25px;visibility: inherit"></div>');
    this.builder.append('<input id="');
    this.builder.append(txtNameId);
    this.builder.append('" type="text" value="');
    this.builder.append(name);
    this.builder.append('" class="u112_input"/>');
    this.builder.append('</div>');
    //更新时间
    this.builder.append('<div class="ax_default box_1 u117" data-label="rLatestTimestamp" ');
    this.builder.append('style="width: 155px; height: 25px; left: 144px; top: ');
    this.builder.append(rowTop);
    this.builder.append('px;visibility: inherit">');
    this.builder.append('<div class="u117_div" style="width: 155px; height: 25px;visibility: inherit"></div>');
    this.builder.append('<div class="text u117_text" style="visibility: inherit">');
    this.builder.append('<p id="');
    this.builder.append(rLatestTimestampId);
    this.builder.append('"><span style="text-decoration:none;">');
    this.builder.append(timestamp);
    this.builder.append('</span></p>');
    this.builder.append('</div>');
    this.builder.append('</div>');
    //是否在线
    this.builder.append('<div class="ax_default droplist u118" data-label="selOnline" ');
    this.builder.append('style="width: 67px; height: 25px; left: 299px; top: ');
    this.builder.append(rowTop);
    this.builder.append('px;visibility: inherit">');
    this.builder.append('<div class="u118_div" style="width: 67px; height: 25px;visibility: inherit"></div>');
    this.builder.append('<select id="');
    this.builder.append(selOnlineId);
    this.builder.append('" class="u118_input">');
    this.builder.append('<option class="u118_input_option" ');
    if (online === 0) {
        this.builder.append('selected ');
    }
    this.builder.append('value="1">否</option>');
    this.builder.append('<option class="u118_input_option" ');
    if (online === 1) {
        this.builder.append('selected ');
    }
    this.builder.append('value="0">是</option>');
    this.builder.append('</select>');
    this.builder.append('</div>');
    //数值类型
    this.builder.append('<div class="ax_default droplist u113" data-label="selDeviceType" ');
    this.builder.append('style="width: 115px; height: 25px; left: 366px; top: ');
    this.builder.append(rowTop);
    this.builder.append('px;visibility: inherit">');
    this.builder.append('<div class="u113_div" style="width: 115px; height: 25px;visibility: inherit"></div>');
    this.builder.append('<select id="');
    this.builder.append(selDataTypeId);
    this.builder.append('" class="u113_input">');
	
    this.builder.append('<option class="u113_input_option" ');
    if (dataType === 0) {
        this.builder.append('selected ');
    }
    this.builder.append('value="0">数值测量值型</option>');
    this.builder.append('<option class="u113_input_option" ');
    if (dataType === 1) {
        this.builder.append('selected ');
    }
    this.builder.append('value="1">开关状态型</option>');
    this.builder.append('<option class="u113_input_option" ');
    if (dataType === 2) {
        this.builder.append('selected ');
    }
    this.builder.append('value="2">地理位置定位型</option>');
    this.builder.append('<option class="u113_input_option" ');
    if (dataType === 3) {
        this.builder.append('selected ');
    }
    this.builder.append('value="3">文本预警消息型</option>"');
	
    this.builder.append('</select>');
    this.builder.append('</div>');
    //修改按钮
    this.builder.append('<div id="');
    this.builder.append(btnModifyDeviceId);
    this.builder.append('" class="ax_default button u114" data-label="btnModifyDevice" ');
    this.builder.append('style="width: 40px; height: 25px; left: 481px; top: ');
    this.builder.append(rowTop);
    this.builder.append('px;visibility: inherit" onclick="onModifyDeviceClick(');
    this.builder.append(index);
    this.builder.append(')">');
    this.builder.append('<div class="u114_div" style="width: 40px; height: 25px;visibility: inherit"></div>');
    this.builder.append('<div class="text u114_text" style="visibility: inherit">');
    this.builder.append('<p><span style="text-decoration:none;">修改</span></p>');
    this.builder.append('</div>');
    this.builder.append('</div>');
    //删除按钮
    this.builder.append('<div id="');
    this.builder.append(btnDeleteDeviceId);
    this.builder.append('" class="ax_default button u115" data-label="btnDeleteDevice" ');
    this.builder.append('style="width: 40px; height: 25px; left: 521px; top: ');
    this.builder.append(rowTop);
    this.builder.append('px;visibility: inherit" onclick="onDeleteDeviceClick(');
    this.builder.append(index);
    this.builder.append(')">');
    this.builder.append('<div class="u115_div" style="width: 40px; height: 25px;visibility: inherit"></div>');
    this.builder.append('<div class="text u115_text" style="visibility: inherit">');
    this.builder.append('<p><span style="text-decoration:none;">删除</span></p>');
    this.builder.append('</div>');
    this.builder.append('</div>');
    //设备行收尾
    this.builder.append('</div>');
}

DeviceTableBuilder.prototype.build = function () {
    this.builder.append('</div>');
    var value = this.builder.toString();
    //console.log(value);
    $("#u111").append(value);
}